﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace TangGuo.UserControls
{
    /// <summary>
    /// NumberBox.xaml 的交互逻辑
    /// </summary>
    public partial class NumberBox : UserControl, IComparable<NumberBox>
    {
        public int CurRow,NewRow;
        public int CurCol,NewCol;
        public int PieceLevel,NewLevel;
        public bool CanJion=true;  // 能否合并。每轮开始时，设置为TRUE，本轮刚合并后，设为FALSE，避免与其他数字再次合并。
        public double[] PosX = { 63, 216, 370, 525 };
        public double[] PosY = { 63, 220, 373, 527 };
        private Storyboard myStoryBoard = new Storyboard() { FillBehavior = FillBehavior.Stop };
        public NumberBox(int initCol = 0, int initRow = 0, int pieceLevel = 0)
        {
            InitializeComponent();
            if ((initCol is < 0 or > 3) || (initRow is < 0 or > 3)) return;
            if (pieceLevel is < 0 or > 10) return;
            SetPosition(initCol, initRow);
            SetNewPositon(initCol, initRow);

            SetLevel(pieceLevel);
            SetNewLevel(pieceLevel);
            
        }

        private void SetLevel(int level)
        {
            PieceLevel = -1;
            if (level is >= 0 and <= 10)
            {
                PieceLevel = level;
                BoxGrid.Background = this.FindResource($"BaoYu_{level}") as ImageBrush; // 设置图片
                this.numberValue.Text=level.ToString();
                Visibility=Visibility.Visible;
            }
            else
            {
                BoxGrid.Background = null;
                Visibility = Visibility.Hidden;
            }
            //NewLevel = PieceLevel;

        }
        public void SetNewLevel(int level)
        {
            NewLevel = level;
        }
        public void SetPosition(int col,int row)
        {
            if ((col is < 0 or > 3) || (row is < 0 or > 3)) return;
            CurCol = col;
            CurRow = row;
            SetValue(Canvas.TopProperty, PosY[row]);
            SetValue(Canvas.LeftProperty, PosX[col]);
        }
        public void SetNewPositon(int col, int row)
        {
            if ((col is < 0 or > 3) || (row is < 0 or > 3)) return;
            NewCol = col;
            NewRow = row;
        }

        public void MoveBoxTo(int targCol, int targRow)
        {
            if ((targCol is <0 or >3) || (targRow is <0 or >3)) return;
            NewRow=targRow; NewCol=targCol;
            DoubleAnimation moveX = new DoubleAnimation()
            {
                To = PosX[targCol],
                Duration = TimeSpan.FromMilliseconds(100)

            };
            Storyboard.SetTargetName(moveX, this.Name);
            Storyboard.SetTargetProperty(moveX,new PropertyPath(Canvas.LeftProperty));
            
            DoubleAnimation moveY = new DoubleAnimation()
            {
                To = PosY[targRow],
                Duration = TimeSpan.FromMilliseconds(100)

            };
            Storyboard.SetTargetName(moveY, this.Name);
            Storyboard.SetTargetProperty(moveY, new PropertyPath(Canvas.TopProperty));

            myStoryBoard.Children.Add(moveX);
            myStoryBoard.Children.Add(moveY);
            myStoryBoard.Completed += MyStoryBoard_Completed;
            myStoryBoard.Begin(this);  // xaml中要给控件命名，否则此处会报错
        }

        private void MyStoryBoard_Completed(object sender, EventArgs e)
        {
            myStoryBoard.Children.Clear();
            myStoryBoard.Remove(this);
            SetPosition(NewCol, NewRow);
            SetLevel(NewLevel);
        }

        public int CompareTo(NumberBox obj)
        {
            return obj.PieceLevel-this.PieceLevel;  // 排序规则，从大到小排序
        }
    }
}
